use std::sync::mpsc;
use std::sync::mpsc::sync_channel;
use std::thread;

fn main() {
    //创建一个通道，缓冲0
    let (sender, receiver) = mpsc::sync_channel::<i32>(0);
    //启动生产者
    thread::spawn(move || {
        for i in 0..5 {
            sender.send(i).expect("Failed to send message");
            println!("Sent message: {}", i);
        }
    });
    //启动接收者
    thread::spawn(move || {
        for _ in 0..5 {
            let received_message = receiver
                .recv()
                .expect("Failed to receive message from receiver");
            println!("Received message: {}", received_message);
        }
    });
    //等待线程结束
    thread::sleep(std::time::Duration::from_secs(10));
}
